package ph.com.gs3.formalistics.model.communicators;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;

import ph.com.gs3.formalistics.global.utilities.logging.FLLogger;
import ph.com.gs3.formalistics.model.valueobjects.application.APIResponse;
import ph.com.gs3.formalistics.model.valueobjects.application.APIResponse.APIInvalidResponseException;
import ph.com.gs3.formalistics.model.valueobjects.business.Comment;
import ph.com.gs3.formalistics.model.valueobjects.business.document.DocumentStub;

public class CommentCommunicatorModule extends HttpCommunicatorModule {

	public static final String TAG = CommentCommunicatorModule.class.getSimpleName();

	public CommentCommunicatorModule(HttpCommunicator communicator) {
		super(communicator);
	}

	public CommentCommunicatorModule(HttpCommunicator communicator, String server) {
		super(communicator, server);
	}

	public APIResponse getFormDocumentsCommentUpdates(String formWebId, String[] documentWebIdList,
	        String lastUpdateDate, int rangeFrom, int rangeTo) throws CommunicatorException,
	        APIInvalidResponseException {

		commonValidation();

		String url = server + "/API/form-requests-comments";

		JSONArray parsedDocumentWebIdList = new JSONArray();

		for (String documentWebId : documentWebIdList) {
			parsedDocumentWebIdList.put(documentWebId);
		}

		Map<String, String> requestParams = new HashMap<>();

		requestParams.put("form_id", formWebId);
		requestParams.put("request_id_list", parsedDocumentWebIdList.toString());
		requestParams.put("range_from", Integer.toString(rangeFrom));
		requestParams.put("range_to", Integer.toString(rangeTo));

		if (lastUpdateDate != null) {
			requestParams.put("last_update_date", lastUpdateDate);
			FLLogger.d(TAG, "Added search parameter, date updated should be >= " + lastUpdateDate);
		} else {
			FLLogger.d(TAG, "Fetching all comments, no last update date set");
		}

		return getResponse(url, requestParams);

	}

	public APIResponse submitComment(Comment comment) throws CommunicatorException,
	        APIInvalidResponseException {

		commonValidation();

		String url = server + "/API/write-comment";

		Map<String, String> requestParams = new HashMap<>();

		requestParams.put("comment", comment.getText());
		requestParams.put("form_id", comment.getDocumentFormWebId());
		requestParams.put("request_id", comment.getDocumentWebId());

		return getResponse(url, requestParams);

	}

	public APIResponse getDocumentCommentUpdates(DocumentStub documentStub)
	        throws CommunicatorException, APIInvalidResponseException {

		commonValidation();

		String url = server + "/API/get-request-comments";

		// prepare the parameter for filtering by date updated

		Map<String, String> requestParams = new HashMap<>();

		requestParams.put("form_id", documentStub.formWebId);
		requestParams.put("request_id", documentStub.webId);

		return getResponse(url, requestParams);

	}

	public APIResponse deleteComment(String commentWebId) throws CommunicatorException,
	        APIInvalidResponseException {

		commonValidation();

		String url = server + "/API/delete-comment";

		Map<String, String> requestParams = new HashMap<>();
		requestParams.put("comment_id", commentWebId);

		return getResponse(url, requestParams);
	}

	public APIResponse getDeletedComments(String lastUpdateDate) throws CommunicatorException,
	        APIInvalidResponseException {

		commonValidation();

		String url = server + "/API/all-deleted-comments";

		Map<String, String> requestParams = new HashMap<>();
		requestParams.put("last_update_date", lastUpdateDate);

		return getResponse(url, requestParams);
	}

}
